package GUILayer;
import CtrlLayer.*;
import ModelLayer.Supplier;

import java.awt.Dimension;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class PopupRedigerLeverandr extends PopupWindow {
	private PersonCtrl PC;
	private Supplier mysupplier;
	private JTextField textFirmanavn;
	private JTextField textKontaktperson;
	private JTextField textAddress;
	private JTextField textpostal;
	private JTextField textCity;
	private JTextField textPhone;
	private JTextField textEmail;
	private JTextField textCreditornr;

	public PopupRedigerLeverandr(Updateable mother)
	{
		super(mother);
		PC = new PersonCtrl();
		getContentPane().setPreferredSize(new Dimension(440, 350));
		setTitle("Rediger Leverandoer");
        
        JLabel lblFirmaNavn = new JLabel("Firmanavn");
        JLabel lblKontaktperson = new JLabel("Kontaktperson");
        JLabel lblAddress = new JLabel("Adresse");
        JLabel lblpostal = new JLabel("Postnr");
        JLabel lblCity = new JLabel("By");
        JLabel lblPhone = new JLabel("Telefon");
        JLabel lblEmail = new JLabel("Email");
        JLabel lblCreditorNr = new JLabel("Creditor nr");
        
        JButton btnAnnuller = new JButton("Annuller");
        btnAnnuller.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e)
        	{
        		quit();
        	}
        });
        JButton btnRet = new JButton("Ret");
        btnRet.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0)
        	{
        		opretLeverandr();
        		getMother().update();
				quit();
        	}
        });
        
        textFirmanavn = new JTextField();
        textFirmanavn.setColumns(10);
        
        textKontaktperson = new JTextField();
        textKontaktperson.setColumns(10);
        
        textAddress = new JTextField();
        textAddress.setColumns(10);
        
        textpostal = new JTextField();
        textpostal.setColumns(10);
        
        textCity = new JTextField();
        textCity.setColumns(10);
        
        textPhone = new JTextField();
        textPhone.setColumns(10);
        
        textEmail = new JTextField();
        textEmail.setColumns(10);
        
        textCreditornr = new JTextField();
        textCreditornr.setColumns(10);
        
        JButton btnSg = new JButton("Søg");
        btnSg.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0)
        	{
        		getLeverandr();
        	}
        });
           
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGap(28)
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(lblEmail)
        				.addComponent(lblFirmaNavn)
        				.addComponent(lblKontaktperson)
        				.addComponent(lblAddress)
        				.addComponent(lblpostal)
        				.addComponent(lblCity, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblPhone)
        				.addComponent(lblCreditorNr))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(textCreditornr, GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
        				.addGroup(groupLayout.createSequentialGroup()
        					.addComponent(btnAnnuller)
        					.addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
        					.addComponent(btnSg)
        					.addGap(27)
        					.addComponent(btnRet))
        				.addComponent(textPhone, 264, 264, 264)
        				.addComponent(textCity, 264, 264, 264)
        				.addComponent(textpostal, 264, 264, 264)
        				.addComponent(textAddress, 264, 264, 264)
        				.addComponent(textKontaktperson, 264, 264, 264)
        				.addComponent(textFirmanavn, 264, 264, 264)
        				.addComponent(textEmail, GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE))
        			.addGap(30))
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblFirmaNavn)
        				.addComponent(textFirmanavn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblKontaktperson)
        				.addComponent(textKontaktperson, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblAddress)
        				.addComponent(textAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblpostal, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textpostal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblCity)
        				.addComponent(textCity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblPhone)
        				.addComponent(textPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblEmail)
        				.addComponent(textEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblCreditorNr)
        				.addComponent(textCreditornr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(27)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(btnAnnuller)
        				.addComponent(btnRet)
        				.addComponent(btnSg))
        			.addContainerGap())
        );
        
        getContentPane().setLayout(groupLayout);
        pack();
	}

	private void opretLeverandr()	//indlaeser text fra skaermen
    {
        String companyName = textFirmanavn.getText();
        String name = textKontaktperson.getText();
        String address = textAddress.getText();
        String postal = textpostal.getText();
        String city = textCity.getText();
        String phone = textPhone.getText();
        String email = textEmail.getText();
        int creditornr = Integer.parseInt(textCreditornr.getText());
        PC.createSupplier(name, address, postal, city, phone, email, companyName, creditornr);
    }
	
	public void getLeverandr()
	{
		String phone = textPhone.getText();
		mysupplier = PC.searchSupplier(phone);
		
		textFirmanavn.setText( mysupplier.getCompanyName() );
		textKontaktperson.setText( mysupplier.getName() );
		textAddress.setText( mysupplier.getAddress() );
		textpostal.setText( mysupplier.getPostal() );
		textCity.setText( mysupplier.getCity() );
		textPhone.setText( mysupplier.getPhone() );
		textEmail.setText( mysupplier.getEmail() );
    	textCreditornr.setText( Integer.toString( mysupplier.getCreditornr() ));
	}
	
	public void resetLeverandr()
	{
		textFirmanavn.removeAll();
		textKontaktperson.removeAll();
		textAddress.removeAll();
		textpostal.removeAll();
		textCity.removeAll();
		textPhone.removeAll();
		textEmail.removeAll();
    	textCreditornr.removeAll();
	}
	
}
